//
//  ByUIWithdrawVC.m
//  chequanbao_ios
//
//  Created by mc on 2017/6/20.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIWithdrawVC.h"

// view
#import "ByViewWithdrawInfo.h"

// cell
#import "ByCellWithdraw.h"

// api
#import "ByApiMine.h"

@interface ByUIWithdrawVC ()<UITableViewDelegate,UITableViewDataSource>
// view
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) ByViewWithdrawInfo *infoview;

// data
@property (nonatomic, strong) NSArray *bankAry;
@property (nonatomic, strong) NSMutableArray *selectArray;
@property (nonatomic, copy)   NSString *minePoints;


@end

@implementation ByUIWithdrawVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收入提现";
    [self.selectArray addObject:@YES];
    [self.selectArray addObject:@NO];
    [self.selectArray addObject:@NO];
    [self.selectArray addObject:@NO];
    [self.selectArray addObject:@NO];
    
    [self addLeftBackBarButtonItem];
    
    [self fetchMinePoints];
}

#pragma mark -- getter
-(NSArray *)bankAry{
    if (!_bankAry) {
        _bankAry = @[@"mine_gongshang",@"mine_zhongguo",@"mine_jiaotong",@"mine_nongye",@"mine_jianshe"];
    }
    return _bankAry;
}
- (NSMutableArray *)selectArray {
    
    if (!_selectArray) {
        _selectArray = [[NSMutableArray alloc] init];
    }
    return _selectArray;
}

#pragma mark -- 获取数据
// 获取车险金
- (void)fetchMinePoints{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    model.code_type = BY_USER.account_id;
    [self performSelector:@selector(afterLoad:) withObject:@"加载中..." afterDelay:0.01];
    [ByApiMine getMinePoints:model success:^(ByNetBaseResult *result) {
        ByModelUser *user = [ByCacheUser getUserInfo];
        user.points = [result.data objectForKey:@"score1"];
        [ByCacheUser setUserInfo:user];
        self.minePoints = [NSString stringWithFormat:@"我的收入：%.2f元",[user.points floatValue]/100.0];
        [self initTable];
        
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];

}

- (void)afterLoad:(NSString *) str{
    [ByCoreToolAlert showLoadind:str];
}


#pragma mark -- 创建视图
- (void)initTable{
    [self createHeaderView];
    
    _infoview = [[ByViewWithdrawInfo alloc] initWithFrame:CGRectMake(0, _headerView.bottom + 10, SCREEN_WIDTH, 6*10 + 50*7)];
    [self.view addSubview:_infoview];
    
    // table1
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, _infoview.bottom + 10, SCREEN_WIDTH, SCREEN_HEIGHT - _infoview.bottom - 10 - 40)];
    [self.view addSubview:_table];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.backgroundColor = [UIColor colorWithWhite:0.95 alpha:0.25];
    _table.delegate = self;
    _table.dataSource = self;
    [self createBottomView];

}

// 创建headerview
- (void)createHeaderView{
    // header
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(10, 64, SCREEN_WIDTH - 20, 50)];
    _headerView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.25];
    _headerView.layer.cornerRadius = 50/2.0;
    
    // 积分
    CGFloat x,y,width,height;
    width = [self.minePoints getSizeOfString:16].width;
    height = 20;
    x = (SCREEN_WIDTH - 20 - width)/2.0 + 25;
    y = 15;
    UILabel *pointLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    pointLabel.text = self.minePoints;
    pointLabel.font = FONT(16);
    pointLabel.textColor = [UIColor whiteColor];
    [_headerView addSubview:pointLabel];
    
    // 图标
    x = CGRectGetMinX(pointLabel.frame) - 25;
    y = y ;
    width = height = 20;
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    img.image = [UIImage imageNamed:@"mine_small_points"];
    [_headerView addSubview:img];
    
    [self.view addSubview:_headerView];
    
    
   
}

// 创建底部button
- (void)createBottomView{
    UIButton *withdrawBtn = [[UIButton alloc] initWithFrame:
                              CGRectMake(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40)];
    withdrawBtn.backgroundColor = BY_THEME_COLOR;
    [withdrawBtn setTitle:@"提现" forState:UIControlStateNormal];
    [self.view addSubview:withdrawBtn];
    
    [withdrawBtn addTarget:self action:@selector(withdraw:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
       return self.bankAry.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellWithdraw *cell = [ByCellWithdraw cellWithTableView:tableView];
    cell.imgStr = self.bankAry[indexPath.section];
    cell.select = [self.selectArray[indexPath.section] boolValue];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section < self.bankAry.count) {
        for (int i = 0; i < self.selectArray.count; i ++) {
            if (i == indexPath.section) {
                [self.selectArray replaceObjectAtIndex:i withObject:@YES];
            } else {
                [self.selectArray replaceObjectAtIndex:i withObject:@NO];
            }
        }
        [self.table reloadData];
    }
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

#pragma mark --
- (void)withdraw:(UIButton *)btn{
//    for (NSString *isSect in self.selectArray) {
//        NSLog(@"%@",isSect);
//        if (isSect.intValue == 1) {
            NSLog(@"%ld",[self.selectArray indexOfObject:@YES]);
//        }
//    }
    
    UIView *nameBgView = [_infoview viewWithTag:100];
    UIView *contactBgView = [_infoview viewWithTag:101];
    UIView *scoreBgView = [_infoview viewWithTag:102];
    UIView *bankNoBgView = [_infoview viewWithTag:103];
    
    UITextField *nameTf = [nameBgView viewWithTag:10];
    UITextField *contactTf = [contactBgView viewWithTag:11];
    UITextField *scoreTf = [scoreBgView viewWithTag:12];
    UITextField *bankNoTf = [bankNoBgView viewWithTag:13];
    
    if (nameTf.text.length == 0 || contactTf.text.length == 0 || scoreTf.text.length == 0 || bankNoTf.text.length == 0 ) {
        [ByCoreToolAlert showOnlyText:@"请填写完整信息"];
        return;
    }
    
    if (scoreTf.text.integerValue < 100) {
        [ByCoreToolAlert showOnlyText:@"提现金额至少为100"];
        return;
    }
    
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    NSInteger index = [self.selectArray indexOfObject:@YES];
    switch (index) {
        case 0:
            model.bank_card_name = @"中国工商银行";
            break;
        case 1:
            model.bank_card_name = @"中国银行";
            break;
        case 2:
            model.bank_card_name = @"交通银行";
            break;
        case 3:
            model.bank_card_name = @"中国农业银行";
            break;
        case 4:
            model.bank_card_name = @"中国建设银行";
            break;
            
        default:
            break;
    }
    
    model.role_type = BY_USER.account_id;
    model.account_id = BY_USER.uid;
//    model.uid = BY_USER.uid;
    model.score = [NSString stringWithFormat:@"%.f",scoreTf.text.floatValue * 100];
    model.bank_card_no = bankNoTf.text;
    model.name = nameTf.text;
    model.mobile = contactTf.text;
    [self performSelector:@selector(afterLoad) withObject:nil afterDelay:0.01];
    [ByApiMine applyforcash:model success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.data];
        [self performSelector:@selector(popVc) withObject:nil afterDelay:1];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
    
    
}

- (void)afterLoad{
    [ByCoreToolAlert showLoadind:@"申请中..."];
}

- (void)popVc{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
